<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <!-- <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button> -->
      <a-button type="primary" icon="download" @click="handleExportXls('项目运维记录表')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <!-- 高级查询区域 -->
      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
        <!-- <span style="float:right;">
          <a @click="loadData()"><a-icon type="sync" />刷新</a>
          <a-divider type="vertical" />
          <list-columns-fix-setter
            v-if="columns.length > 0"
            v-model="columns"
            :def-columns="columns"
            @change="columnChange"
          />
        </span> -->
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange"
        click-row-show-sub-form>

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text,record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record" style="display: flex;">
          <a @click="handleDetail(record)">详情</a>
          <!-- <a-divider type="vertical" />
          <a @click="openSubMenu(record)">子菜单</a> -->
          <!-- <a-divider type="vertical" />
          <a-menu-item>
                <a @click="handleDetail(record)">{{record.maintenanceStatus==2||!record.maintenanceStatus?'详情':'变更'}}</a>
          </a-menu-item> -->
          <!-- <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">变更/详情</a>
              </a-menu-item>
              
            </a-menu>
          </a-dropdown> -->
          <!-- <a-divider type="vertical" />
          <a-menu-item>
                <a-popconfirm :title="record.maintenanceStatus==2||!record.maintenanceStatus?'确定完成吗?':'确定放弃吗?'" @confirm="() => handleDelete(record.id)">
                  <a>{{record.maintenanceStatus==2||!record.maintenanceStatus?'完成':'放弃'}}</a>
                </a-popconfirm>
              </a-menu-item> -->
        </span>
        <!-- 子表单 -->
      <!-- <template v-slot:subForm="{record}">
        <template v-if="loadSubData(record)">
          <j-vxe-table
            ref="subFormTable"
            height="auto"
            :max-height="350"
            :loading="subTable.loading"
            :columns="subTable.columns"
            :dataSource="subTable.dataSource"
          />
        </template>
      </template> -->

      </a-table>
    </div>

    <yks-equipment-devops-history-modal ref="modalForm" @ok="modalFormOk"></yks-equipment-devops-history-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import YksEquipmentDevopsHistoryModal from './modules/YksEquipmentDevopsHistoryModal'
  import { httpAction, getAction } from '@/api/manage'

  export default {
    name: 'YksEquipmentDevopsHistoryList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      YksEquipmentDevopsHistoryModal
    },
    data () {
      return {
        description: '项目运维记录表管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'名称',
            align:"center",
            dataIndex: 'name'
          },
          // {
          //   title:'运维单名称',
          //   align:"center",
          //   dataIndex: 'devopsId_dictText'
          // },
          {
            title:'项目',
            align:"center",
            dataIndex: 'equipmentId_dictText'
          },
          {
            title:'运维类型',
            align:"center",
            dataIndex: 'devopsPersontype_dictText'
          },
          {
            title:'运维人员',
            align:"center",
            dataIndex: 'maintenancePerson_dictText'
          },
          {
            title:'运维部门',
            align:"center",
            dataIndex: 'maintenanceDept_dictText'
          },
          {
            title:'触发类型',
            align:"center",
            dataIndex: 'triggerType_dictText'
          },
          {
            title:'初次触发时间',
            align:"center",
            dataIndex: 'triggerTime',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'重复次数',
            align:"center",
            dataIndex: 'repeatNumber'
          },
          {
            title:'重复间隔',
            align:"center",
            dataIndex: 'repeatGap'
          },
          {
            title:'重复间隔单位',
            align:"center",
            dataIndex: 'repeatGapUnit_dictText'
          },
          {
            title:'实际保养时间',
            align:"center",
            dataIndex: 'maintenanceActualTime',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'计划保养时间',
            align:"center",
            dataIndex: 'maintenancePlanTime',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'保养时间段开始',
            align:"center",
            dataIndex: 'maintenanceQuantumstart',
            customRender:function (text) {

return !text?"":(text.length>10?text.substr(0,10):text)
}
          },
          {
            title:'保养时间段结束',
            align:"center",
            dataIndex: 'maintenanceQuantumend',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'保养类型',
            align:"center",
            dataIndex: 'maintenanceType_dictText'
          },
          {
            title:'保养内容',
            align:"center",
            dataIndex: 'maintenanceContext'
          },
          {
            title:'保养明细',
            align:"center",
            dataIndex: 'templateId_dictText'
          },
          {
            title:'保养项目',
            align:"center",
            dataIndex: 'maintenanceItem',
            // customRender:function (text) {
            //   return !text?"":(text.length>10?text.substr(0,10):text)
            // }
            customRender:function (text) {
              return '点击详情查看明细保养项目'
              
              // return "text"//!text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'运维状态',
            align:"center",
            dataIndex: 'maintenanceStatus_dictText',
            customRender:function (text) {
              return !text||text==''?"待接单":text
            }
          }
          ,
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/dev/yksEquipmentDevopsHistory/list",
          // list: "/dev/yksEquipmentDevops/list",
          delete: "/dev/yksEquipmentDevopsHistory/delete",
          deleteBatch: "/dev/yksEquipmentDevopsHistory/deleteBatch",
          exportXlsUrl: "/dev/yksEquipmentDevopsHistory/exportXls",
          importExcelUrl: "dev/yksEquipmentDevopsHistory/importExcel",
          
        },
        dictOptions:{},
        superFieldList:[],
        // 子表的信息
        subTable: {
          currentRowId: null,
          loading: false,
          pagination: {current: 1, pageSize: 200, pageSizeOptions: ['100', '200'], total: 0},
          selectedRows: [],
          dataSource: [],
          columns: [
            {key: 'name', title: '保养项目', width: '120px'},
            {key: 'detailName', title: '明细', width: '180px'},
            {key: 'detailType', title: '明细类别', width: '180px'},
            {key: 'detailContext', title: '明细内容', width: '180px'},
            // {key: 'type', title: '船舶分类', width: '120px', type: JVXETypes.input},
            // {key: 'port_area', title: '所属港区', minWidth: '120px', type: JVXETypes.input},
          ],
        },
        currentEquipmentId:''
      }
    },
    created() {
    this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      // 查询子表数据
      loadSubData(row) {
        debugger
        if (row) {
          // 这里一定要做限制，限制不能重复查询，否者会出现死循环
          if (this.subTable.currentRowId === row.id) {
            return true
          }
          this.subTable.currentRowId = row.id
          let formData = {pageNo: 1, pageSize: 30, parentId: row.id}
          let contexts=JSON.parse(row.maintenanceItem)
          
          let details=[]
              for(var i=0;i<this.contexts.length;i++){
                let content={}
                content.name= this.contexts[i].name//+':\r\n'
                let context=this.contexts[i]
                for(var j=0;j<context.details.length;j++){
                  let detail=context.details[j]
                  content.detailName=detail.name
                  content.detailType=detail.controlType
                  content.detailContext=detail.controlValue
                  if(detail.controlType=='3'||detail.controlType=='4'){
                    let selectValue=detail.controls.filter(p=>p.id==detail.selectid)
                    content.detailContext="选择"+selectValue.value//+'\r\n'

                  }
                  
                }
                details.push(content)
              }
              this.subTable.dataSource=details

          // this.subTable.loading = true
          // getAction(this.url.getData, formData).then(res => {
          //   if (res.success) {
          //     // 将查询的数据赋值给 dataSource
          //     this.subTable.dataSource = res.result.records
          //   } else {
          //     this.$error({title: '主表查询失败', content: res.message})
          //   }
          // }).finally(() => {
          //   // 这里是无论成功或失败都会执行的方法，在这里关闭loading
          //   this.subTable.loading = false
          // })
          return true
        } else {
          return false
        }
      },
      open(record) {
      this.currentEquipmentId = record.id
      this.loadData(1)
    },
    loadData(arg) {
      debugger
      if (!this.url.list) {
        this.$message.error("请设置url.list属性!")
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1;
      }
      var params = this.getQueryParams();//查询条件
      params.equipmentId = this.currentEquipmentId
      // params.maintenanceNextTime=new Date().getDate()
      this.loading = true;
      getAction(this.url.list, params).then((res) => {
        if (res.success) {
          //update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
          this.dataSource = res.result.records || res.result;
          if (res.result.total) {
            this.ipagination.total = res.result.total;
          } else {
            this.ipagination.total = 0;
          }
          //update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
        } else {
          this.$message.warning(res.message)
        }
      }).finally(() => {
        this.loading = false
      })
    },
      initDictConfig(){
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'name',text:'名称',dictCode:''})
        fieldList.push({type:'string',value:'devopsId',text:'运维id',dictCode:'yks_equipment_devops,name,id'})
        fieldList.push({type:'string',value:'equipmentId',text:'项目',dictCode:"yks_equipment,name,id"})
        fieldList.push({type:'string',value:'devopsPersontype',text:'运维类型',dictCode:'devops_persontype'})
        fieldList.push({type:'string',value:'maintenancePerson',text:'运维人员',dictCode:'sys_user,username,id'})
        fieldList.push({type:'string',value:'maintenanceDept',text:'运维部门',dictCode:'sys_depart,depart_name,id'})
        fieldList.push({type:'string',value:'triggerType',text:'触发类型',dictCode:'trigger_type'})
        fieldList.push({type:'date',value:'triggerTime',text:'初次触发时间'})
        fieldList.push({type:'string',value:'repeatNumber',text:'重复次数',dictCode:''})
        fieldList.push({type:'string',value:'repeatGap',text:'重复间隔',dictCode:''})
        fieldList.push({type:'string',value:'repeatGapUnit',text:'重复间隔单位',dictCode:'yks_unit,name,id'})
        fieldList.push({type:'date',value:'maintenanceActualTime',text:'实际保养时间'})
        fieldList.push({type:'date',value:'maintenancePlanTime',text:'计划保养时间'})
        fieldList.push({type:'date',value:'maintenanceQuantumstart',text:'保养时间段开始'})
        fieldList.push({type:'date',value:'maintenanceQuantumend',text:'保养时间段结束'})
        fieldList.push({type:'string',value:'maintenanceType',text:'保养类型',dictCode:'maintenance_type'})
        fieldList.push({type:'string',value:'maintenanceContext',text:'保养内容',dictCode:''})
        fieldList.push({type:'string',value:'templateId',text:'模版ID',dictCode:"yks_maintenance_template,template_name,id"})
        fieldList.push({type:'string',value:'maintenanceItem',text:'保养项目',dictCode:''})
        fieldList.push({type:'int',value:'maintenanceStatus',text:'运维状态',dictCode:'maintenance_status'})
        this.superFieldList = fieldList
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>